package com.titanrise.crm.mapper;

import com.titanrise.crm.domain.dto.TradeDto;
import com.titanrise.crm.domain.dto.TradeTableDto;
import com.titanrise.crm.domain.model.Trade;
import com.titanrise.crm.environment.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description:
 * @Author: pront
 * @Time:2024-11-06 17:15
 */
@Mapper
public interface TradeMapper {

    @Select("SELECT trade.trade_id, trade.price, trade.goods_name,\n" +
            "    trade.finish_date, trade.update_name, trade.update_time,\n" +
            "    trade.note, trade.status, trade.number,\n" +
            "    salesperson_role.name AS salesperson_name,\n" +
            "    client_role.name AS client_name\n" +
            "FROM  tb_trade AS trade\n" +
            "LEFT JOIN \n" +
            "    tb_user AS salesperson_user ON trade.salesperson_id = salesperson_user.id\n" +
            "LEFT JOIN \n" +
            "    tb_user AS client_user ON trade.client_id = client_user.id\n" +
            "LEFT JOIN \n" +
            "    tb_role AS salesperson_role ON salesperson_user.id = salesperson_role.uid\n" +
            "LEFT JOIN \n" +
            "    tb_role AS client_role ON client_user.id = client_role.uid\n" +
            "ORDER BY  trade.status ASC;\n" +
            "\n")
    @ResultType(TradeDto.class)
    List<TradeDto> selectAllDto();

    @Select("SELECT trade.trade_id, trade.price, trade.goods_name,\n" +
            "    trade.finish_date, trade.update_name, trade.update_time,\n" +
            "    trade.note, trade.status, trade.number,\n" +
            "    salesperson_role.name AS salesperson_name,\n" +
            "    client_role.name AS client_name\n" +
            "FROM  tb_trade AS trade\n" +
            "LEFT JOIN \n" +
            "    tb_user AS salesperson_user ON trade.salesperson_id = salesperson_user.id\n" +
            "LEFT JOIN \n" +
            "    tb_user AS client_user ON trade.client_id = client_user.id\n" +
            "LEFT JOIN \n" +
            "    tb_role AS salesperson_role ON salesperson_user.id = salesperson_role.uid\n" +
            "LEFT JOIN \n" +
            "    tb_role AS client_role ON client_user.id = client_role.uid\n" +
            "WHERE trade.salesperson_id = #{sid}\n" +
            "ORDER BY trade.status ASC;\n")
    @ResultType(TradeDto.class)
    List<TradeDto> selectDtoBySalespersonIO(@Param("sid") String sid);

    @Delete("delete from tb_trade where trade_id = #{tradeId}")
    int deleteById(@Param("tradeId") String tradeId);

    @Select("select * from tb_trade where salesperson_id = #{salespersonId}")
    @ResultType(Trade.class)
    List<Trade> selectBySalespersonId(@Param("salespersonId") String salespersonId);


    @Select("SELECT r.name AS name, COUNT(t.trade_id) AS total_orders, SUM(t.price) AS total_sales_price " +
            "FROM tb_trade t JOIN tb_role r ON t.salesperson_id = r.uid " +
            "WHERE t.status > 1 GROUP BY r.name")
    @ResultType(TradeTableDto.class)
    List<TradeTableDto> selectTradeTable();

    @Update("UPDATE tb_trade SET status = #{status}")
    int updateStatus(@Param("status") Integer status);

    @Select("select trade_id from tb_trade where client_id = #{clientId} and salesperson_id = #{salesId} and goods_name = #{goodsName}")
    String selectTradeCountByClientIdAndSalesIdAndSkuName(@Param("clientId") String clientId, @Param("salesId") String salesId, @Param("goodsName") String goodsName);

    @Update("update tb_trade set status = #{status}, update_name = #{updateName}, price = #{price} where trade_id = #{tradeId}")
    int updateTradeStatusById(@Param("tradeId") String tradeId, @Param("status") int status, @Param("updateName") String updateName, @Param("price") BigDecimal price);

    @Insert("insert tb_trade(trade_id, salesperson_id, goods_name, client_id, status, number, price)" +
            "value(#{trade.tradeId}, #{trade.salespersonId}, #{trade.goodsName}, #{trade.clientId}, #{trade.status}, #{trade.number}, #{trade.price})")
    int insertOne(@ParamEntity("trade") Trade trade);

    @Select("select * from tb_trade where trade_id = #{tradeId}")
    @ResultType(Trade.class)
    Trade selectById(@Param("tradeId") String tradeId);

    @Update("update tb_trade set note = #{note} where trade_id = #{tradeId}")
    int updateTradeNodeByTradeId(@Param("tradeId") String tradeId, @Param("note")String note);

    @Delete("delete from tb_trade where client_id = #{clientId}")
    int deleteTradeByClientUid(@Param("clientId") String clientId);

    @Select("select * from tb_trade where client_id = #{clientId} and status = 2")
    @ResultType(Trade.class)
    List<Trade> selectAllConfirmDealTradeByClientId(@Param("clientId") String clientId);

    @Select("select count(*) from tb_trade where client_id = #{clientId}")
    int selectTradeCountByClientId(String clientId);

    @Select("select * from tb_trade where client_id = #{clientId}")
    @ResultType(Trade.class)
    List<Trade> selectAllTradeByClientId(@Param("clientId") String clientId);
}
